﻿using B.S.DataBase.MyProject.API.Appicant.Command;
using B.S.DataBase.MyProject.API.DTOs;
using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.UserCommandHandler
{
    public class GetUserCommandHandler : IRequestHandler<GetUserCommand, APIResult<List<UserDTOs>>>
    {
        private readonly IBaseRepository<User> userRepos;
        private readonly IBaseRepository<Role> roleRepos;
        private readonly IBaseRepository<UserRole> userRoleRepos;

        public GetUserCommandHandler(
            IBaseRepository<User> userRepos,
            IBaseRepository<Role> roleRepos,
            IBaseRepository<UserRole> userRoleRepos

            )
        {
            this.userRepos = userRepos;
            this.roleRepos = roleRepos;
            this.userRoleRepos = userRoleRepos;
        }

        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<APIResult<List<UserDTOs>>> Handle(GetUserCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserDTOs>> result = new APIResult<List<UserDTOs>>();
            result.Code = APIEnum.操作成功;
            result.Msg = APIEnum.操作成功.ToString();


            #region 我的User显示
            //var list = userRepos.GetAll().Result.Where(x=>x.IsDel == false);
            //var lian = from a in userRepos.GetAll().Result
            //           join b in userRoleRepos.GetAll().Result on a.UserId equals b.UserId
            //           join c in roleRepos.GetAll().Result on b.RoleId equals c.RoleId
            //           where a.IsDel == false
            //           select new UserDTOs
            //           {
            //               UserId = a.UserId,
            //               DepId = a.UserId,
            //               UserName = a.UserName,
            //               Password = a.Password,
            //               NickName = a.NickName,
            //               Sex = a.Sex,
            //               ProvinceId = a.ProvinceId,
            //               CityId = a.CityId,
            //               CountyId = a.CountyId,
            //               HamletName = a.HamletName,
            //               CreateName = a.CreateName,
            //               CreateTime = a.CreateTime,
            //               UpdateName = a.UpdateName,
            //               UpdateTime = a.UpdateTime,
            //               IsDel = a.IsDel,
            //               RoleName = c.RoleName,
            //           };
            //result.data = lian.ToList();
            #endregion


            var list = userRepos.GetAll().Result
    .Join(userRoleRepos.GetAll().Result, a => a.UserId, b => b.UserId, (a, b) => new { a, b })
    .Join(roleRepos.GetAll().Result, temp => temp.b.RoleId, o => o.RoleId, (temp, o) => new UserDTOs
    {
        UserId = temp.a.UserId,
        UserName = temp.a.UserName,
        DepId = temp.a.DepId,
        NickName = temp.a.NickName,
        Sex = temp.a.Sex,
        ProvinceId = temp.a.ProvinceId,
        CityId = temp.a.CityId,
        CountyId = temp.a.CountyId,
        HamletName = temp.a.HamletName,
        RoleId = o.RoleId,
        RoleName = o.RoleName,
        UserRoleId = temp.b.UserRoleId,
        CreateName = temp.a.CreateName,
        CreateTime = temp.a.CreateTime,
        UpdateName = temp.a.UpdateName,
        UpdateTime = temp.a.UpdateTime,
        IsDel = temp.a.IsDel,
    }).GroupBy(x => x.UserId) // 按用户编号分组
            .Select(group => new UserDTOs
            {
                UserId = group.Key,
                UserName = group.First().UserName,
                DepId = group.First().DepId,
                NickName = group.First().NickName,
                Sex = group.First().Sex,
                ProvinceId = group.First().ProvinceId,
                CityId = group.First().CityId,
                CountyId = group.First().CountyId,
                HamletName = group.First().HamletName,
                CreateName = group.First().CreateName,
                CreateTime = group.First().CreateTime,
                UpdateName = group.First().UpdateName,
                UpdateTime = group.First().UpdateTime,
                RoleId = group.First().RoleId,
                RoleName = string.Join(", ", group.Select(x => x.RoleName)), // 合并角色名称
                UserRoleId = group.First().UserRoleId,
                IsDel = group.First().IsDel
            }).Where(x => x.IsDel == false);
            result.data = list.ToList();
            return await Task.FromResult(result);
        }
    }
}
